home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / ibus / inputcontext.py < prev    next >
Text File  |  2009-11-05  |  11KB  |  323 lines

  1. # vim:set et sts=4 sw=4:
  2. #
  3. # ibus - The Input Bus
  4. #
  5. # Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
  6. #
  7. # This library is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU Lesser General Public
  9. # License as published by the Free Software Foundation; either
  10. # version 2 of the License, or (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. # GNU Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public
  18. # License along with this program; if not, write to the
  19. # Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20. # Boston, MA  02111-1307  USA
  21.  
  22. __all__ = (
  23.         "InputContext",
  24.     )
  25.  
  26. import sys
  27. import gobject
  28. import dbus
  29. import dbus.lowlevel
  30. import object
  31. import common
  32. import serializable
  33.  
  34. class InputContext(object.Object):
  35.     __gtype_name__ = "PYIBusInputContext"
  36.     __gsignals__ = {
  37.         "commit-text" : (
  38.             gobject.SIGNAL_RUN_LAST,
  39.             gobject.TYPE_NONE,
  40.             (gobject.TYPE_PYOBJECT, )
  41.         ),
  42.         "update-preedit-text" : (
  43.             gobject.SIGNAL_RUN_LAST,
  44.             gobject.TYPE_NONE,
  45.             (gobject.TYPE_PYOBJECT, gobject.TYPE_UINT, gobject.TYPE_BOOLEAN)
  46.         ),
  47.         "show-preedit-text" : (
  48.             gobject.SIGNAL_RUN_LAST,
  49.             gobject.TYPE_NONE,
  50.             ()
  51.         ),
  52.         "hide-preedit-text" : (
  53.             gobject.SIGNAL_RUN_LAST,
  54.             gobject.TYPE_NONE,
  55.             ()
  56.         ),
  57.         "update-auxiliary-text" : (
  58.             gobject.SIGNAL_RUN_LAST,
  59.             gobject.TYPE_NONE,
  60.             (gobject.TYPE_PYOBJECT, gobject.TYPE_BOOLEAN)
  61.         ),
  62.         "show-auxiliary-text" : (
  63.             gobject.SIGNAL_RUN_LAST,
  64.             gobject.TYPE_NONE,
  65.             ()
  66.         ),
  67.         "hide-auxiliary-text" : (
  68.             gobject.SIGNAL_RUN_LAST,
  69.             gobject.TYPE_NONE,
  70.             ()
  71.         ),
  72.         "update-lookup-table" : (
  73.             gobject.SIGNAL_RUN_LAST,
  74.             gobject.TYPE_NONE,
  75.             (gobject.TYPE_PYOBJECT, gobject.TYPE_BOOLEAN)
  76.         ),
  77.         "show-lookup-table" : (
  78.             gobject.SIGNAL_RUN_LAST,
  79.             gobject.TYPE_NONE,
  80.             ()
  81.         ),
  82.         "hide-lookup-table" : (
  83.             gobject.SIGNAL_RUN_LAST,
  84.             gobject.TYPE_NONE,
  85.             ()
  86.         ),
  87.         "page-up-lookup-table" : (
  88.             gobject.SIGNAL_RUN_LAST,
  89.             gobject.TYPE_NONE,
  90.             ()
  91.         ),
  92.         "page-down-lookup-table" : (
  93.             gobject.SIGNAL_RUN_LAST,
  94.             gobject.TYPE_NONE,
  95.             ()
  96.         ),
  97.         "cursor-up-lookup-table" : (
  98.             gobject.SIGNAL_RUN_LAST,
  99.             gobject.TYPE_NONE,
  100.             ()
  101.         ),
  102.         "cursor-down-lookup-table" : (
  103.             gobject.SIGNAL_RUN_LAST,
  104.             gobject.TYPE_NONE,
  105.             ()
  106.         ),
  107.         "enabled" : (
  108.             gobject.SIGNAL_RUN_LAST,
  109.             gobject.TYPE_NONE,
  110.             ()
  111.         ),
  112.         "disabled" : (
  113.             gobject.SIGNAL_RUN_LAST,
  114.             gobject.TYPE_NONE,
  115.             ()
  116.         ),
  117.     }
  118.  
  119.     def __init__(self, bus, path, watch_signals=False):
  120.         super(InputContext, self).__init__()
  121.  
  122.         self.__bus = bus
  123.         self.__context = bus.get_dbusconn().get_object(common.IBUS_SERVICE_IBUS, path)
  124.         self.__signal_matches = []
  125.  
  126.         if not watch_signals:
  127.             return
  128.  
  129.         m = self.__context.connect_to_signal("CommitText", self.__commit_text_cb)
  130.         self.__signal_matches.append(m)
  131.         m = self.__context.connect_to_signal("UpdatePreeditText", self.__update_preedit_text_cb)
  132.         self.__signal_matches.append(m)
  133.         m = self.__context.connect_to_signal("UpdateAuxiliaryText", self.__update_auxiliary_text_cb)
  134.         self.__signal_matches.append(m)
  135.         m = self.__context.connect_to_signal("UpdateLookupTable", self.__update_lookup_table_cb)
  136.         self.__signal_matches.append(m)
  137.  
  138.         m = self.__context.connect_to_signal("Enabled",             lambda *args: self.emit("enabled"))
  139.         self.__signal_matches.append(m)
  140.         m = self.__context.connect_to_signal("Disabled",            lambda *args: self.emit("disabled"))
  141.         self.__signal_matches.append(m)
  142.         m = self.__context.connect_to_signal("ShowPreeditText",     lambda *args: self.emit("show-preedit-text"))
  143.         self.__signal_matches.append(m)
  144.         m = self.__context.connect_to_signal("HidePreeditText",     lambda *args: self.emit("hide-preedit-text"))
  145.         self.__signal_matches.append(m)
  146.         m = self.__context.connect_to_signal("ShowAuxiliaryText",   lambda *args: self.emit("show-auxiliary-text"))
  147.         self.__signal_matches.append(m)
  148.         m = self.__context.connect_to_signal("HideAuxiliaryText",   lambda *args: self.emit("hide-auxiliary-text"))
  149.         self.__signal_matches.append(m)
  150.         m = self.__context.connect_to_signal("ShowLookupTable",     lambda *args: self.emit("show-lookup-table"))
  151.         self.__signal_matches.append(m)
  152.         m = self.__context.connect_to_signal("HideLookupTable",     lambda *argss: self.emit("hide-lookup-table"))
  153.         self.__signal_matches.append(m)
  154.         m = self.__context.connect_to_signal("PageUpLookupTable",   lambda *args: self.emit("page-up-lookup-table"))
  155.         self.__signal_matches.append(m)
  156.         m = self.__context.connect_to_signal("PageDownLookupTable", lambda *args: self.emit("page-down-lookup-table"))
  157.         self.__signal_matches.append(m)
  158.         m = self.__context.connect_to_signal("CursorUpLookupTable", lambda *args: self.emit("cursor-up-lookup-table"))
  159.         self.__signal_matches.append(m)
  160.         m = self.__context.connect_to_signal("CursorDownLookupTable", lambda *args: self.emit("cursor-down-lookup-table"))
  161.         self.__signal_matches.append(m)
  162.  
  163.     def __commit_text_cb(self, *args):
  164.         text = serializable.deserialize_object(args[0])
  165.         self.emit("commit-text", text)
  166.  
  167.     def __update_preedit_text_cb(self, *args):
  168.         text = serializable.deserialize_object(args[0])
  169.         cursor_pos = args[1]
  170.         visible = args[2]
  171.         self.emit("update-preedit-text", text, cursor_pos, visible)
  172.  
  173.     def __update_auxiliary_text_cb(self, *args):
  174.         text = serializable.deserialize_object(args[0])
  175.         visible = args[1]
  176.         self.emit("update-auxiliary-text", text, visible)
  177.  
  178.     def __update_lookup_table_cb(self, *args):
  179.         table = serializable.deserialize_object(args[0])
  180.         visible = args[1]
  181.         self.emit("update-lookup-table", table, visible)
  182.  
  183.     def process_key_event(self, keyval, keycode, modifiers):
  184.         keyval = dbus.UInt32(keyval)
  185.         keycode = dbus.UInt32(keycode)
  186.         modifiers = dbus.UInt32(modifiers)
  187.         return self.__context.ProcessKeyEvent(keyval, keycode, modifiers)
  188.  
  189.     def set_cursor_location(self, x, y, w, h):
  190.         x = dbus.Int32(x)
  191.         y = dbus.Int32(y)
  192.         w = dbus.Int32(w)
  193.         h = dbus.Int32(h)
  194.         return self.__context.SetCursorLocation(x, y, w, h)
  195.  
  196.     def focus_in(self):
  197.         return self.__context.FocusIn()
  198.  
  199.     def focus_out(self):
  200.         return self.__context.FocusOut()
  201.  
  202.     def reset(self):
  203.         return self.__context.Reset()
  204.  
  205.     def enable(self):
  206.         return self.__context.Enable()
  207.  
  208.     def disable(self):
  209.         return self.__context.Disable()
  210.  
  211.     def is_enabled(self):
  212.         return self.__context.IsEnabled()
  213.  
  214.     def set_capabilities(self, caps):
  215.         caps = dbus.UInt32(caps)
  216.         return self.__context.SetCapabilities(caps)
  217.  
  218.     def detach_signals(self):
  219.         for m in self.__signal_matches:
  220.             m.remove()
  221.         del self.__signal_matches[:]
  222.  
  223.     def destroy(self):
  224.         self.detach_signals()
  225.         self.__context.Destroy()
  226.         super(InputContext, self).destroy()
  227.  
  228.     def get_engine(self):
  229.         try:
  230.             engine = self.__context.GetEngine()
  231.             engine = serializable.deserialize_object(engine)
  232.             return engine
  233.         except:
  234.             return None
  235.  
  236.     def set_engine(self, engine):
  237.         return self.__context.SetEngine(engine.name)
  238.  
  239.     def introspect(self):
  240.         return self.__context.Introspect()
  241.  
  242.  
  243.  
  244. def test():
  245.     import gtk
  246.     import gtk.gdk
  247.     from bus import Bus
  248.     import modifier
  249.     import text
  250.     import attribute
  251.     import property
  252.     import lookuptable
  253.     import factory
  254.  
  255.     class TestWindow(gtk.Window):
  256.         def __init__(self):
  257.             super(TestWindow,self).__init__()
  258.  
  259.             self.__bus = Bus()
  260.             print self.__bus.get_name()
  261.             self.__bus.connect("disconnected", gtk.main_quit)
  262.             context_path = self.__bus.create_input_context("Test")
  263.             print context_path
  264.             self.__context = InputContext(self.__bus, context_path)
  265.             self.__context.set_capabilities (9)
  266.  
  267.             self.__context.connect("commit-text", self.__commit_text_cb)
  268.             self.__context.connect("update-preedit-text", self.__update_preedit_text_cb)
  269.             self.__context.connect("show-preedit-text", self.__show_preedit_text_cb)
  270.             self.__context.connect("update-auxiliary-text", self.__update_auxiliary_text_cb)
  271.             self.__context.connect("update-lookup-table", self.__update_lookup_table_cb)
  272.             self.__context.connect("enabled", self.__enabled_cb)
  273.             self.__context.connect("disabled", self.__disabled_cb)
  274.  
  275.             self.set_events(gtk.gdk.KEY_PRESS_MASK | gtk.gdk.KEY_RELEASE_MASK | gtk.gdk.FOCUS_CHANGE_MASK)
  276.  
  277.             self.connect("key-press-event", self.__key_press_event_cb)
  278.             self.connect("key-release-event", self.__key_release_event_cb)
  279.             self.connect("delete-event", gtk.main_quit)
  280.             self.connect("focus-in-event", lambda *args: self.__context.focus_in())
  281.             self.connect("focus-out-event", lambda *args: self.__context.focus_out())
  282.  
  283.             self.show_all()
  284.  
  285.         def __commit_text_cb(self, context, text):
  286.             print "commit-text:", text.text
  287.  
  288.         def __update_preedit_text_cb(self, context, text, cursor_pos, visible):
  289.             print "preedit-text:", text.text, cursor_pos, visible
  290.  
  291.         def __show_preedit_text_cb(self, context):
  292.             print "show-preedit-text"
  293.  
  294.         def __hide_preedit_text_cb(self, context):
  295.             print "hide-preedit-text"
  296.  
  297.         def __update_auxiliary_text_cb(self, context, text, visible):
  298.             print "auxiliary-text:", text.text, visible
  299.  
  300.         def __update_lookup_table_cb(self, context, table, visible):
  301.             print "update-lookup-table:", visible
  302.  
  303.         def __enabled_cb(self, context):
  304.             print "enabled"
  305.             info = context.get_factory_info()
  306.             print "factory = %s" % info.name
  307.  
  308.         def __disabled_cb(self, context):
  309.             print "disabled"
  310.  
  311.         def __key_press_event_cb(self, widget, event):
  312.             self.__context.process_key_event(event.keyval, event.state)
  313.  
  314.         def __key_release_event_cb(self, widget, event):
  315.             self.__context.process_key_event(event.keyval, event.state | modifier.RELEASE_MASK)
  316.  
  317.     w = TestWindow()
  318.     gtk.main()
  319.  
  320. if __name__ == "__main__":
  321.     test()
  322.  
  323.